package com.whmskj.xjlhbc.mobile.controller;

import cn.dev33.satoken.annotation.SaIgnore;
import com.whmskj.xjlhbc.core.AjaxResult;
import com.whmskj.xjlhbc.core.Page;
import com.whmskj.xjlhbc.dto.DailyProductionQueryDto;
import com.whmskj.xjlhbc.dto.MonthlyProductionQueryDto;
import com.whmskj.xjlhbc.service.IBctj14ProductionMonthlyCompletionService;
import com.whmskj.xjlhbc.service.ICompletionOfProductionDayBctj14Service;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.media.Content;
import io.swagger.v3.oas.annotations.media.Schema;
import io.swagger.v3.oas.annotations.responses.ApiResponse;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@RestController
@Tag(name = "月和日产量完成情况", description = "月和日产量完成情况管理")
@RequestMapping("/ClMonthAndDaily")
@SaIgnore
public class ClMonthOfDailyController {

    @Autowired
    private ICompletionOfProductionDayBctj14Service dailyService;

    @Autowired
    private IBctj14ProductionMonthlyCompletionService monthlyService;

    /**
     * 获取月度产量报告
     */
    @GetMapping("/monthlyReport")
    @Operation(description = "获取月度产量报告")
    public AjaxResult getMonthlyReport(
            @RequestParam(required = false) Integer year,
            @RequestParam(required = false) Integer month,
            @RequestParam(required = false) String startDate,
            @RequestParam(required = false) String endDate) {
        try {
            MonthlyProductionQueryDto queryDto = new MonthlyProductionQueryDto();
            queryDto.setYear(year);
            queryDto.setMonth(month);
            queryDto.setStartDate(startDate);
            queryDto.setEndDate(endDate);

            Map<String, Object> report = monthlyService.getMonthlyReport(queryDto);
            return AjaxResult.success("查询成功", report);
        } catch (Exception e) {
            return AjaxResult.error("查询失败: " + e.getMessage());
        }
    }

    /**
     * 获取每日产量完成情况 - 整经
     */
    @GetMapping("/daily/warping")
    @Operation(description = "获取每日产量完成情况 - 整经")
    public AjaxResult getDailyWarping(DailyProductionQueryDto queryDto) {
        try {
            queryDto.setProductionType("整经");

            // 如果指定了年月，按年月查询；否则按日期范围查询
            if (queryDto.getYear() != null && queryDto.getMonth() != null) {
                List<Map<String, Object>> statistics = dailyService.getWarpingStatisticsByMonth(
                        queryDto.getYear(), queryDto.getMonth());
                return AjaxResult.success("查询成功", statistics);
            } else {
                List<Map<String, Object>> statistics = dailyService.getWarpingStatistics(
                        queryDto.getStartDate(), queryDto.getEndDate());
                return AjaxResult.success("查询成功", statistics);
            }
        } catch (Exception e) {
            return AjaxResult.error("查询失败: " + e.getMessage());
        }
    }

    /**
     * 获取每日产量完成情况 - 浆纱
     */
    @GetMapping("/daily/sizing")
    @Operation(description = "获取每日产量完成情况 - 浆纱")
    public AjaxResult getDailySizing(DailyProductionQueryDto queryDto) {
        try {
            queryDto.setProductionType("浆纱");

            // 如果指定了年月，按年月查询；否则按日期范围查询
            if (queryDto.getYear() != null && queryDto.getMonth() != null) {
                List<Map<String, Object>> statistics = dailyService.getSizingStatisticsByMonth(
                        queryDto.getYear(), queryDto.getMonth());
                return AjaxResult.success("查询成功", statistics);
            } else {
                List<Map<String, Object>> statistics = dailyService.getSizingStatistics(
                        queryDto.getStartDate(), queryDto.getEndDate());
                return AjaxResult.success("查询成功", statistics);
            }
        } catch (Exception e) {
            return AjaxResult.error("查询失败: " + e.getMessage());
        }
    }

    /**
     * 获取每日产量完成情况 - 整理
     */
    @GetMapping("/daily/zl")
    @Operation(description = "获取每日产量完成情况 - 整理")
    public AjaxResult getDailyZl(DailyProductionQueryDto queryDto) {
        try {
            queryDto.setProductionType("整理");

            // 如果指定了年月，按年月查询；否则按日期范围查询
            if (queryDto.getYear() != null && queryDto.getMonth() != null) {
                List<Map<String, Object>> statistics = dailyService.getZlStatisticsByMonth(
                        queryDto.getYear(), queryDto.getMonth());
                return AjaxResult.success("查询成功", statistics);
            } else {
                List<Map<String, Object>> statistics = dailyService.getZlStatistics(
                        queryDto.getStartDate(), queryDto.getEndDate());
                return AjaxResult.success("查询成功", statistics);
            }
        } catch (Exception e) {
            return AjaxResult.error("查询失败: " + e.getMessage());
        }
    }

    /**
     * 获取每日产量完成情况 - 织布
     */
    @GetMapping("/daily/weaving")
    @Operation(description = "获取每日产量完成情况 - 织布")
    public AjaxResult getDailyWeaving(DailyProductionQueryDto queryDto) {
        try {
            queryDto.setProductionType("织布");

            // 如果指定了年月，按年月查询；否则按日期范围查询
            if (queryDto.getYear() != null && queryDto.getMonth() != null) {
                List<Map<String, Object>> statistics = dailyService.getWeavingStatisticsByMonth(
                        queryDto.getYear(), queryDto.getMonth());
                return AjaxResult.success("查询成功", statistics);
            } else {
                List<Map<String, Object>> statistics = dailyService.getWeavingStatistics(
                        queryDto.getStartDate(), queryDto.getEndDate());
                return AjaxResult.success("查询成功", statistics);
            }
        } catch (Exception e) {
            return AjaxResult.error("查询失败: " + e.getMessage());
        }
    }


    /**
     * 获取每日产量完成情况列表
     */
    @GetMapping("/daily/list")
    @Operation(description = "获取每日产量完成情况列表")
    @ApiResponse(content = @Content(
            schema = @Schema(anyOf = {AjaxResult.class, Object.class})
    ))
    public AjaxResult getDailyList(DailyProductionQueryDto queryDto) {
        try {
            List<Map<String, Object>> list = dailyService.queryList(queryDto);
            return Page.getDataTable(list);
        } catch (Exception e) {
            return AjaxResult.error("查询失败: " + e.getMessage());
        }
    }

    /**
     * 获取月度产量完成情况列表
     */
    @GetMapping("/monthly/list")
    @Operation(description = "获取月度产量完成情况列表")
    @ApiResponse(content = @Content(
            schema = @Schema(anyOf = {AjaxResult.class, Object.class})
    ))
    public AjaxResult getMonthlyList(MonthlyProductionQueryDto queryDto) {
        try {
            List<Map<String, Object>> list = monthlyService.queryList(queryDto);
            return Page.getDataTable(list);
        } catch (Exception e) {
            return AjaxResult.error("查询失败: " + e.getMessage());
        }
    }
}
